/*
 * Sketching JavaScript API v1.1
 *
 */

/*****************************************
 * public functions
 */
function sketchGetVersion() {
    return "RunSketch v0026; Library v1.2";
}
function sketchIOPackGetDeviceNames() {
    var names = new Array();
    for (n in NadaCore.__ioPackInputValuesByDevice) if ((n != "remote") && (n != "local")) names[names.length] = n;
    return names;
}
function sketchShowCameraPreviewInBackground() {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.showCameraPreviewInBackground();
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_showCameraPreviewInBackground
	    ]);
	}
}
function sketchHideCameraPreview() {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.hideCameraPreview();
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_hideCameraPreview
	    ]);
	}
}
function sketchIOPackRead(device, port) {
    if (typeof NadaCore.__ioPackInputValuesByDevice[device] == "undefined") return "none";
    if ((port < 0) || (port >= NadaCore.__ioPackInputValuesByDevice[device].length)) return "none";
    return NadaCore.__ioPackInputValuesByDevice[device][port];
}
function sketchIOPackWrite(device, port, value) {
    var v = new Array();
    for (var i = 0; i <= 9; i++)
        v[i] = (i != port) ? sketchIOPackRead(device, i) : value;
    sketchSetIOPack(device, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]);
}
function sketchSetIOPack(device, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9) {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.setIOPack(device, v0, v1, v2, v3, v4, v5, v6, v7, v8, v9);
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_setIOPack,
            device,
	        /*Math.min(255, Math.max(0, v0)),
	        Math.min(255, Math.max(0, v1)),
	        Math.min(255, Math.max(0, v2)),
	        Math.min(255, Math.max(0, v3)),
	        Math.min(255, Math.max(0, v4)),
	        Math.min(255, Math.max(0, v5)),
	        Math.min(255, Math.max(0, v6)),
	        Math.min(255, Math.max(0, v7)),
	        Math.min(255, Math.max(0, v8)),
	        Math.min(255, Math.max(0, v9))*/
            v0,
            v1,
            v2,
            v3,
            v4,
            v5,
            v6,
            v7,
            v8,
            v9
	    ]);
	}
}
function sketchProximitySensorEnable() {
    sketchSetProximitySensor(true);
}
function sketchProximitySensorDisable() {
    sketchSetProximitySensor(false);
}
function sketchSetProximitySensor(state) {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.setProximitySensor(state);
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_setProximitySensor, state
	    ]);
	}
}
function sketchSensorToneEnable() {
    sketchSetSensorTone(true);
}
function sketchSensorToneDisable() {
    sketchSetSensorTone(false);
}
function sketchSetSensorTone(state) {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.setSensorTone(state);
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_setSensorTone, state
	    ]);
	}
}
function sketchSoundPlay(url) {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.soundPlay(url);
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_soundPlay, url
	    ]);
	}
}
function sketchSoundRecord() {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.soundRecord();
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_soundRecord
	    ]);
	}
}
function sketchVideoPlay(url) {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.videoPlay(url);
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_videoPlay, url
	    ]);
	}
}
function sketchCameraRead() {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.cameraRead();
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_cameraRead
	    ]);
	}
}
function sketchTweet(username, password, message, imageUrl) {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.sketchTweet(username, password, message, imageUrl);
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_tweet, username, password, message, imageUrl
	    ]);
	}
}
function sketchSocketAccept() {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.sketchSocketAccept();
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_socketAccept
	    ]);
	}
}
function sketchSocketSend(device, msg) {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.sketchSocketSend(device, msg);
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_socketSend, device, msg
	    ]);
	}
}
function sketchPeerListen(myName) {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.sketchPeerListen(myName);
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_peerListen, myName
	    ]);
	}
}
function sketchPeerSend(toName, myName, msg) {
	if (NadaCore.__useNativeInterface) {
		NadaCore_ni.sketchPeerSend(toName, myName, msg);
	} else {
	    NadaCore.__reverseCall([
	        NadaCore.REVERSE_FCN_peerSend, toName, myName, msg
	    ]);
	}
}

var NadaCore = {
    /*****************************************
     * function opcodes
     */
    REVERSE_FCN_setIOPack:          100,
    REVERSE_FCN_soundPlay:          101,
    REVERSE_FCN_soundRecord:        102,
    REVERSE_FCN_setSensorTone:      103,
    REVERSE_FCN_setProximitySensor: 104,
    REVERSE_FCN_cameraRead:         105,
    REVERSE_FCN_tweet:              106,
    REVERSE_FCN_socketAccept:       107,
    REVERSE_FCN_socketSend:         108,
    REVERSE_FCN_peerListen:         109,
    REVERSE_FCN_peerSend:           110,
    REVERSE_FCN_videoPlay:          111,
    REVERSE_FCN_showCameraPreviewInBackground: 112,
    REVERSE_FCN_hideCameraPreview:  113,

    /*****************************************
     * reverse call ivars and functions
     */
    __useNativeInterface: false,
    
    __ioPackInputValuesByDevice: new Object(), //IOPack input value buffer
    
    __rcq: new Array(), //reverse call queue
    __escapeArg: function(arg) {
        //NadaCore.__debugMsg("Called NadaCore.__escapeArg");
        return arg.replace(",","");
    },
    __reverseCall: function(calldesc) {
        //NadaCore.__debugMsg("Called NadaCore.__reverseCall "+calldesc[0]);
        //this.__rcq.push(calldesc);
        this.__rcq[calldesc[0]] = calldesc;
    },
    __skc_poll: function() {
        //NadaCore.__debugMsg("Called NadaCore.__skc_poll");
        /*if (this.__rcq.length <= 0) return "";
        //return this.__rcq.pop().toString();
        var calldesc = this.__rcq.shift();
        */
        if (typeof this.__rcq[this.REVERSE_FCN_setIOPack] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_setIOPack];
            this.__rcq[this.REVERSE_FCN_setIOPack] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_soundPlay] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_soundPlay];
            this.__rcq[this.REVERSE_FCN_soundPlay] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_soundRecord] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_soundRecord];
            this.__rcq[this.REVERSE_FCN_soundRecord] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_setSensorTone] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_setSensorTone];
            this.__rcq[this.REVERSE_FCN_setSensorTone] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_setProximitySensor] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_setProximitySensor];
            this.__rcq[this.REVERSE_FCN_setProximitySensor] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_cameraRead] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_cameraRead];
            this.__rcq[this.REVERSE_FCN_cameraRead] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_tweet] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_tweet];
            this.__rcq[this.REVERSE_FCN_tweet] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_socketAccept] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_socketAccept];
            this.__rcq[this.REVERSE_FCN_socketAccept] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_socketSend] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_socketSend];
            this.__rcq[this.REVERSE_FCN_socketSend] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_peerListen] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_peerListen];
            this.__rcq[this.REVERSE_FCN_peerListen] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_peerSend] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_peerSend];
            this.__rcq[this.REVERSE_FCN_peerSend] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_videoPlay] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_videoPlay];
            this.__rcq[this.REVERSE_FCN_videoPlay] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_showCameraPreviewInBackground] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_showCameraPreviewInBackground];
            this.__rcq[this.REVERSE_FCN_showCameraPreviewInBackground] = undefined;
        } else if (typeof this.__rcq[this.REVERSE_FCN_hideCameraPreview] != "undefined") {
            var calldesc = this.__rcq[this.REVERSE_FCN_hideCameraPreview];
            this.__rcq[this.REVERSE_FCN_hideCameraPreview] = undefined;
        }
        var s = "";
        for (var i = 0; i < calldesc.length; i++) {
            s += calldesc[i] + "|";
        }
        //NadaCore.__debugMsg("NadaCore.__skc_poll returning "+s);
        return s;
    },
    __debugMsg: function(msg) {
        if (typeof __debug != "undefined") __debug.innerHTML = msg+"<br />"+__debug.innerHTML.substr(0, 1000);
    },
    __sketchOnIOPackInput: function(device, v) {
        this.__ioPackInputValuesByDevice[device] = v;
        if (device.substr(0, 6) == "remote") {
            this.__ioPackInputValuesByDevice["remote"] = v;
        } else {
            this.__ioPackInputValuesByDevice["local"] = v;
        }
        if (typeof sketchOnIOPackInput != "undefined") sketchOnIOPackInput(device, v[0], v[1], v[2], v[3], v[4], v[5], v[6], v[7], v[8], v[9]);
    }
};

if (typeof NadaCore_ni != "undefined") NadaCore.__useNativeInterface = true;

//call the user's setup callback
if (typeof sketchSetup != "undefined") sketchSetup();

/*******************************************************/


